<?php
/**
 * 获取配置的接口
 */
namespace app\api\controller;
use app\common\controller\Api;

class Config extends Api {
	//设置允许通过API请求获得的配置(如果不传指定参数，讲返回所有配置)
	protected $allow_default_config_list = [
        'config_version' => 'api_config_version',//api版本
        'filter' => 'filter_list',//过滤
        'stream_notice' => 'stream_notice',//直播间公告
        'im' => 'webim',//聊天服务器配置
        'filter_word_config' => 'filter_word_conf',//敏感词过滤
        //'gift' => 'gift_list', //礼物兑换列表,
	];
    protected $allow_get_config_list = [
        'gift' => 'gift_list', //礼物兑换列表,
        'filter_word' => 'filter_word_list', //敏感词配置列表
    ];
    protected $down_conf_list = [
        0 => 'filter_word',//敏感词配置列表
    ];//下载的配置
	/**
	 * 未定接口
	 */
	public function _empty() {
		//返回信息
		return getApiErrorCode('00404');
	}
	/**
	 * @name 检测请求的api来源
	 * @author martinsun syh@sunyonghong.com
	 * @access protected
	 * @param boolean $auth 是否验证token
	 * @param array $must 必须的参数
	 * @param string $method 请求方式  默认不限制
	 * @return boolean|array  返回信息
	 */
	protected function _checkApi($method = '', $must = ['token', 'hextime'], $auth = true) {
		//请求方法验证
		if ($method) {
			$method = (strtolower($method) == 'get') ? (IS_GET ? true : false):(IS_POST ? true : false);
			if ($method === false) {
				return getApiErrorCode('00100');
			}
		}
		//必须参数验证
		if (!empty($must)) {
			$m_count = count($must);
			$r_count = count(array_intersect_key(array_flip($must), $this->data));
			if ($m_count > $r_count) {
				return getApiErrorCode('40001');
			}
		}
		//权限验证
		if ($auth === true) {
			//加密算法验证
			$ctime = hexdec($this->data['hextime']);
			//收到请求的时间不能大于60s
			if (time() - 60 > $ctime || $ctime > time()) {
				return getApiErrorCode('40423');
			}
			//转为小写
			$_token = strtolower(md5($ctime . $this->data['hextime']));
			if (strtolower($this->data['token']) != $_token) {
				return getApiErrorCode('40423');
			}
		}

		return true;

	}
	/**
	 * @name 配置版本对比的接口
	 */
	public function getApiVersion() {
		$st = self::_checkApi('', ['hextime', 'token'], true);
		if ($st !== true) {
			return $st;
		}
        $conf = model('admin/Config')->ApiServiceGetConf('config_version');
		return ['code' => '00000', 'data' => current($conf)];
	}
	
    /**
     * @name 根据API版本获取API接口地址
     */
    public function getApi(){
        $st = self::_checkApi('', ['hextime', 'token'],true);
		if ($st !== true) {
			return $st;
		}
        $api_version = $this->data['api_version'];
        switch ($api_version){
            case '1.0':
                return ['code' => '00000','data'=>'http://live.shante.me/api'];
            default:
                return ['code' => '00000','data'=>'http://live.shante.me/api'];
        }
    }
	/**
	 * 获得配置信息接口
	 *
	 */
	public function get() {
		$st = self::_checkApi('', ['hextime', 'token'],true);
		if ($st !== true) {
			return $st;
		}
		//反解析获得name参数
		$name = array_filter(explode(',', urldecode(base64_decode($this->data['name']))));
		//默认返回所有允许获得的配置
		if (empty($name)) {
			$name = $this->allow_default_config_list;
		}
		$name = array_unique($name);
        $configList = array_merge($this->allow_default_config_list,$this->allow_get_config_list);
		$config = [];
		//获取配置
		foreach ($name as $v) {
			$v = self::_replace($v, '/^[A-Z]$/');
			if ($type = array_search($v, $configList)) {
			     if(in_array($type,$this->down_conf_list)){
                    return $this->downConfig($type);
                }
				$conf = model('admin/Config')->ApiServiceGetConf($type);
                
                if($conf){
                    $config[$v] = ((arrayLevel($conf) === 1) && (count($conf) === 1)) ? current($conf) : $conf;
                }else{
                    $config[$v] = []; 
                }
			}
		}
		return empty($config) ? getApiErrorCode('40423') : ['code' => '00000', 'data' => $config];

	}
	/**
	 * 字符串替换方法
	 */
	protected function _replace($str = '', $reg = '', $replace = '_') {
		$leng = strlen($str);
		for ($i = 0; $i < $leng; $i++) {
			$char = substr($str, $i, 1);
			if (preg_match($reg, $char)) {
				$str = str_replace($char, $replace . strtolower($char), $str);
			}
		}
		return $str;
	}
    private function downConfig($config_name = ''){

	$down = new \app\common\model\Download('public/confdata/'.$config_name.'.zip');
        $down->getfiles();
        exit;
    }
}
